package com.demo.rydemo.d_resource.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.rydemo.d_resource.entity.DResource;
import com.demo.rydemo.d_resource.service.IDResourceService;
import com.demo.rydemo.result.AjaxResult;
import com.demo.rydemo.result.CoreController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ry
 * @since 2020-08-20
 */
@Api(tags = "资源管理接口")
@RestController
@RequestMapping("/d_resource/d-resource")
@Slf4j
public class DResourceController  extends CoreController {

    @Autowired
    AjaxResult ajaxResult;

    @Autowired
    IDResourceService idResourceService;

    @ApiOperation("获取根节点接口")
    @GetMapping("/getResRoot")
    AjaxResult<DResource> getResRoot(){
        DResource resource = idResourceService.getById(1);
        if(resource != null){
            return ajaxResult.success(resource);
        }else{
            return ajaxResult.error("获取根节点失败！");
        }
    }


    @ApiOperation("获取下级节点，通过父节点ID")
    @GetMapping("/getSubNode")
    AjaxResult getSubNode(String id){

        QueryWrapper<DResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("resource_parent",id).eq("status","1");
        List<DResource> list = idResourceService.list(queryWrapper);
        if(list != null && list.size() > 0){
            return ajaxResult.success(list);
        }else{
            return ajaxResult.error("获取根节点失败！");
        }
    }

    @ApiOperation("添加资源")
    @PostMapping("/addRes")
    AjaxResult addRes(@RequestBody DResource dResource){
        Boolean flag = false;
        flag = idResourceService.addRes(dResource);
        if(flag){
            return ajaxResult.success(flag);
        }
        return ajaxResult.error("错误，请联系管理员");
    }

    @ApiOperation("获取资源详情通过ID")
    @GetMapping("getResById")
    AjaxResult getResById(String id){
        DResource resource = idResourceService.getById(id);
        if(resource != null){
            return ajaxResult.success(resource);
        }
        return ajaxResult.error("错误，获获取资源详情错误");
    }

    @ApiOperation("修改资源详情")
    @PostMapping("/resUpd")
    AjaxResult resUpd(@RequestBody DResource dResource){
        Boolean flag = false;

        flag = idResourceService.updateById(dResource);
        if(flag){
            return ajaxResult.success("修改成功!");
        }
        return ajaxResult.error("修改失败!");
    }

    @ApiOperation("获取所有资源")
    @GetMapping("/getAllRes")
    AjaxResult getAllRes(){
        QueryWrapper<DResource> qw = new QueryWrapper();
        qw.orderByAsc("resource_parent");
        return ajaxResult.success(idResourceService.list(qw));
    }

}
